package de.uni.muenster.idw2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import de.uni.muenster.idw2.data.BBBuilding;
import de.uni.muenster.idw2.data.Building;
import de.uni.muenster.idw2.interfaces.BuildingMapInterface;
import de.uni.muenster.idw2.location.LocationController;
import de.uni.muenster.idw2.threads.BuildingLoaderThread;
import de.uni.muenster.idw2.vis.MapView;

public class BuildingMap extends Activity implements BuildingMapInterface{
	
	private Building building = null;
	private BuildingLoaderThread buildingLoader;
	private FrameLayout frameLayoutBuilding;
	private MapView mapView = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.building_view_map);
		
		
		frameLayoutBuilding = (FrameLayout) findViewById(R.id.viewLayout);
		
		/**
		 * get The building xml
		 */
		BBBuilding bbBuilding = (BBBuilding) getIntent().getExtras().getSerializable(BuildingChooserMap.MAP_ACTIVITY_RESULT_DATA);
		buildingLoader = new BuildingLoaderThread(bbBuilding, this);
		buildingLoader.execute();
		
		
		
	}
	
    public void info(View view){
    	if(building != null){
			Intent intent = new Intent(this, BuildingInformation.class);
	    	intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	    	startActivity(intent);
			}
			else{
				Toast.makeText(this, "NO USEFUL BUILDING INFORMATION", 4).show();
			}
    }
    
    public void navigate(View view){
    	if(building != null){
    		Intent intent = new Intent(this, RouteSelector.class);
        	intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        	startActivity(intent);
    		}
    		else{
    			Toast.makeText(this, "NO USEFUL BUILDING INFORMATION", 4).show();
    		}
    }
	
	@Override
	public void loadMap(Building building) {
		this.building = building;
		
		LocationController.startAllLocationUpdates(building, this);
		// mapview.enableReceiver
		
		// @Philipp: Whereever you call the MapView with constructor including the path/route, 
		//           use those two lines of code below to calculate the route 
		mapView  = new MapView(this, building);
		mapView.enableReceiver(this);

		frameLayoutBuilding.addView(mapView);
	}
	
	public void nextFloor(View view){
		if(mapView != null){
			if (mapView.isNextLevelAvailable()){
				mapView.nextLevel();
			}
		}			
	}
	
	public void prevFloor(View view){
		if(mapView != null){
			if (mapView.isPrevLevelAvailable()){
				mapView.prevLevel();
			}
		}
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		if (buildingLoader != null && !buildingLoader.isCancelled()) {
			buildingLoader.cancel(true);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		LocationController.stopAllLocationUpdates(this);
	}
	
}
